package com.jm.web.form.support;
import javax.servlet.jsp.JspWriter;

import com.jm.utils.AttributeBuilder;
import com.jm.utils.StringHelper;
import com.jm.web.form.Button;
import com.jm.web.form.Input;
import com.jm.web.form.Select;
import com.jm.web.form.baseitem.CheckboxItem;
import com.jm.web.form.baseitem.RadioItem;
import com.jm.web.form.baseitem.SelectItem;

/**
 * @author yanjie
 *
 * 2011/9/19 上午11:28:18
 * 带编辑功能控件
 */
public abstract class EditItem extends FormItem{
	/**
	 * @param tag
	 */
	public EditItem() {}
	
	private static String TAG_DISABLED = "disabled";
	private static String TAG_READONLY = "readonly";
	private static String TAG_ENAB = "enab";
	
	protected boolean bDisabled = false;
	protected boolean bReadonly = false;
	protected boolean bEnab = true;
	
	
	@Override
	public void onRender(JspWriter writer) {
		if(this.isEnab() ||  !(this instanceof Button))
			super.onRender(writer);
	}
	
	public void OnSetProperty(String strName, String strValue){
		if (strName.compareToIgnoreCase(TAG_DISABLED) == 0)
		{
			if("disabled".equalsIgnoreCase(strValue))
				this.bDisabled = true ;
			else
				this.bDisabled = StringHelper.GetValue(strValue,bDisabled);
			
			return;
		}
		
		if (strName.compareToIgnoreCase(TAG_READONLY) == 0)
		{
			if("readonly".equalsIgnoreCase(strValue)){
				this.bReadonly = true;
			}else{
				this.bReadonly = StringHelper.GetValue(strValue,bReadonly);
			}
			return;
		}
		
		if (strName.compareToIgnoreCase(TAG_ENAB) == 0)
		{
			this.bEnab = StringHelper.GetValue(strValue,bEnab);;
			if(!this.bEnab){
				this.bReadonly = true;
			}
			return;
		}
		
		super.OnSetProperty(strName,strValue);
	}
	
	@Override
	public void FillAttributeBuilder(AttributeBuilder attributesBuilder){
		super.FillAttributeBuilder(attributesBuilder);
		if(this.isDisabled())
			attributesBuilder.Set(TAG_DISABLED,"disabled");
		
		if(this.isReadonly())
			attributesBuilder.Set(TAG_READONLY.toLowerCase(),TAG_READONLY.toLowerCase());
		
		if(!this.isEnab()){
			attributesBuilder.Set(TAG_ENAB,"false");
			if(!this.isFromTag()){
				attributesBuilder.Append("class","ax-disabled");
//				&& StringHelper.IsNullOrEmpty(this.GetExtValue("xtype",""))
				if(this instanceof Input
						&& !("radio".equalsIgnoreCase(((Input)this).getType()) || "checkbox".equalsIgnoreCase(((Input)this).getType()))
					){
					attributesBuilder.Set(TAG_READONLY.toLowerCase(),"true");
				}else{
					attributesBuilder.Set(TAG_DISABLED.toLowerCase(),"true");
				}
			}
			
			if (this instanceof RadioItem 
					|| this instanceof CheckboxItem
					|| this instanceof SelectItem) {
				//註冊radio checkbox select 的 reanonly
				if(!this.getBaseTag().getPage().isRegisterReadonlyJs(this.getId())){
					this.getBaseTag().getPage().addReadonlyNode(this.getId());
				}
			}else if(this instanceof Input || this instanceof Select){
				if(this instanceof Select 
						||"radio".equalsIgnoreCase(((Input)this).getType())
						||"checkbox".equalsIgnoreCase(((Input)this).getType())){
					//註冊radio checkbox select 的 reanonly
					if(!this.getBaseTag().getPage().isRegisterReadonlyJs(this.getId())){
						this.getBaseTag().getPage().addReadonlyNode(this.getId());
					}
				}
//				else {
//					
//					if(!this.getBaseTag().getPage().isRegisterReadonlyJs(this.getId())){
//						this.getBaseTag().getPage().addReadonlyNode(this.getId());
//					}
//				}
			}
		}
	}

	public boolean isDisabled() {
		return bDisabled;
	}

	public void setDisabled(boolean disabled) {
		bDisabled = disabled;
	}

	public boolean isEnab() {
		return bEnab;
	}

	public void setEnab(boolean enab) {
		bEnab = enab;
	}

	public boolean isReadonly() {
		return bReadonly;
	}

	public void setReadonly(boolean readonly) {
		bReadonly = readonly;
	}

}
